<?php


namespace App\Models;


use App\Services\CommonService;
use App\Services\ZlbService;
use Illuminate\Database\Eloquent\Model;

class ZlbGoods extends Model
{
    protected $table = "goods";
    protected $primaryKey = 'id';
    public $timestamps = false;

    /**
     * 商品上链新增数据
     * @param $goods
     * @param $shopname
     * @param $zlb_class
     * @param $goodsSetmeal
     * @param $version
     */
    public static function add($orderId,$goods,$shopname,$zlb_class,$goodsSetmeal,$version){
        $goodsDepict = obToArray(GoodsDepict::where(['goods_id'=>$goods['id']])->first());
        $res = ZlbService::categoryClass($zlb_class);
        $data = [
            'order_id'=>$orderId,
            'mainClass'=>$res['mainClass'],
            'subClass'=>$res['subClass'],
            'goods_id'=>$goods['id'],
            'version_id'=>$version,
            'name'=>$goods['title'],
            'shop_id'=>$goods['member_id'],
            'product_origin'=>2,
            'info'=>$goodsDepict['goods_content'],
            'brand_name'=>$shopname,
            'az_price'=>0,
            'goods_img'=>$goods['imgurl'],
            'create_time'=>dateFormat(time()),
//            'month'=>$goodsSpec['lease_time'],
//            'unit_price'=>$goodsSpec['lease_price'],
            'number'=>1,
            'images'=>$goodsDepict['images'],
            'setmeal_id'=>$goodsSetmeal?$goodsSetmeal['id']:$goods['id'],
            'is_buy_out'=>$goodsSetmeal?$goodsSetmeal['is_buy_out']:'1',
            'market_price'=>$goodsSetmeal?$goodsSetmeal['price']:$goods['min_price'],
            'price'=>$goodsSetmeal?$goodsSetmeal['price']:$goods['min_price'],
            'foregift'=>$goodsSetmeal?$goodsSetmeal['foregift']:$goods['min_price_g'],
            'stock'=>$goodsSetmeal?$goodsSetmeal['quantity']:999,
            'estimated'=>$goodsSetmeal?$goodsSetmeal['quantity']:999,
            'sku_name'=>$goodsSetmeal?$goodsSetmeal['title']:$goods['title'],
        ];
        $id = ZlbGoods::insertGetId($data);
        // var_dump($id);
    }

    /**
     * 商品上链
     * @param $order_id
     * @param $request
     * @param $notary
     */
    public static function set($order_id,$request,$notary){
        $res = ZlbService::format($notary);
        if($res['status'] == ZlbService::STATUS_YES){
            //成功
            ZlbService::requestLog('上传商品成功',$request,$res);
        }else{
            //失败
            ZlbService::requestLog('上传商品失败',$request,$res);
        }
        $data = [
            'chain_status'=>$res['status'],
            'chain_time'=>dateFormat(time()),
            'chain_params'=>ZlbService::paramsResponse($request,$notary),
        ];
        ZlbGoods::where(['order_id'=>$order_id])->update($data);
    }

}
